package com.iplant.wms.serviceImpl.dao.mss;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.po.mss.MSSMeasureUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class MSSMeasureUnitDAO extends BaseDAO {

	private static Logger logger = LoggerFactory.getLogger(MSSMeasureUnitDAO.class);
	private static MSSMeasureUnitDAO Instance = null;

	private MSSMeasureUnitDAO() {
		super();

		setTableName("mss_measureunit");
	}

	public static MSSMeasureUnitDAO getInstance() {
		if (Instance == null)
			Instance = new MSSMeasureUnitDAO();
		return Instance;
	}

	public void MSS_UpdateMeasureUnit(BMSEmployee wLoginUser, MSSMeasureUnit wMeasureUnit,
			OutResult<Integer> wErrorCode) {
		wErrorCode.set(0);

		try {
			if (wMeasureUnit == null || wMeasureUnit.MeasureUnitID <= 0 || wMeasureUnit.SourceUnitID <= 0) {
				wErrorCode.set(ExceptionTypes.Parameter.getValue());
				return;
			}
			MSSMeasureUnit wMeasureUnitO = this.MSS_CheckMeasureUnit(wLoginUser, wMeasureUnit, wErrorCode);
			if (wMeasureUnitO.ID > 0) {
				wErrorCode.set(ExceptionTypes.Duplication.getValue());
			}
			if (wErrorCode.Result != 0)
				return;

			Map<String, Object> wParwms = new HashMap<String, Object>();

			wParwms.put("Description", wMeasureUnit.Description);
			wParwms.put("MeasureUnitID", wMeasureUnit.MeasureUnitID);
			wParwms.put("SourceUnitID", wMeasureUnit.SourceUnitID);
			wParwms.put("Coefficient", wMeasureUnit.Coefficient);
			wParwms.put("EditorID", wLoginUser.getID());
			wParwms.put("EditTime", Calendar.getInstance());
			wParwms.put("Active", wMeasureUnit.Active);

			if (wMeasureUnit.ID > 0) {
				wParwms.put("ID", wMeasureUnit.ID);
				super.Update(super.getTableName("mss_measureunit"), "ID", wParwms);
			} else {
				wParwms.put("CreatorID", wLoginUser.getID());
				wParwms.put("CreateTime", Calendar.getInstance());
				wMeasureUnit.ID = super.Insert(super.getTableName("mss_measureunit"), wParwms);
			}

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("MSS_UpdateMeasureUnit", ex);
		}
	}

	public void MSS_ActiveMeasureUnit(BMSEmployee wLoginUser, MSSMeasureUnit wMeasureUnit, int wActive,
			OutResult<Integer> wErrorCode) {
		wErrorCode.set(0);
		// 判断客户信息是否存在(中国：统一社会信用代码，国外:提醒是否有重复）

		try {
			if (wMeasureUnit == null || wMeasureUnit.ID <= 0) {
				wErrorCode.set(ExceptionTypes.Parameter.getValue());
				return;
			}

			Map<String, Object> wParwms = new HashMap<String, Object>();

			wParwms.put("ID", wMeasureUnit.ID);
			wParwms.put("EditorID", wLoginUser.getID());
			wParwms.put("EditTime", Calendar.getInstance());
			wParwms.put("Active", wActive);
			super.Update(super.getTableName("mss_measureunit"), "ID", wParwms);

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());

			logger.error("MSS_ActiveMeasureUnit", ex);
		}
	}

	public List<MSSMeasureUnit> MSS_QueryMeasureUnitList(BMSEmployee wLoginUser, List<Integer> wMeasureUnitID,
			List<Integer> wSourceUnitID, int wActive, Pagination wPagination, OutResult<Integer> wErrorCode) {
		List<MSSMeasureUnit> wMSSMeasureUnitList = new ArrayList<MSSMeasureUnit>();
		wErrorCode.set(0);

		try {

			// Step02:查询
			Map<String, Object> wParwms = new HashMap<String, Object>();
			String wSQLText = "";
			wSQLText = StringUtils.Format(
					"Select t.*,t1.Name as MeasureUnitName,t1.Code as MeasureUnitCode, t2.Name as SourceUnitName,t2.Code as SourceUnitCode ,"
							+ " t3.Name as CreatorName ,t4.Name as EditorName  from {0}.mss_measureunit t  "
							+ " left join {0}.mss_unit t1 on t.MeasureUnitID=t1.ID "
							+ " left join {0}.mss_unit t2 on t.SourceUnitID=t2.ID "
							+ " left join {0}.mbs_user t3 on t.CreatorID=t3.ID "
							+ " left join {0}.mbs_user t4 on t.EditorID=t4.ID "
							+ " where (:Active<0 or  t.Active=:Active)   AND (:MeasureUnitID = '''' or  t.MeasureUnitID in ({1})) "
							+ " AND (:SourceUnitID = '''' or  t.SourceUnitID in ({2}))  ",
					super.getDBName(),
					wMeasureUnitID.size() > 0 ? StringUtils.Join(",", wMeasureUnitID) : "0",
					wSourceUnitID.size() > 0 ? StringUtils.Join(",", wSourceUnitID) : "0");

			wSQLText = this.DMLChange(wSQLText);

			wParwms.put("SourceUnitID", StringUtils.Join(",", wSourceUnitID));
			wParwms.put("MeasureUnitID", StringUtils.Join(",", wMeasureUnitID));
			wParwms.put("Active", wActive);

			wMSSMeasureUnitList = super.QueryForList(wSQLText, wParwms, wPagination, MSSMeasureUnit.class);

			// Step03:配置系统
		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("MSS_QueryMeasureUnitList", ex);
		}
		return wMSSMeasureUnitList;
	}

	public MSSMeasureUnit MSS_QueryMeasureUnit(BMSEmployee wLoginUser, Integer wMeasureUnitID, Integer wSourceUnitID,
			OutResult<Integer> wErrorCode) {
		MSSMeasureUnit wResult = new MSSMeasureUnit();
		wErrorCode.set(0);

		try {
			if (wMeasureUnitID <= 0 || wSourceUnitID <= 0)
				return wResult;
			List<MSSMeasureUnit> wList = this.MSS_QueryMeasureUnitList(wLoginUser,
					StringUtils.parseListArgs(wMeasureUnitID), StringUtils.parseListArgs(wSourceUnitID), -1,
					Pagination.getDefault(), wErrorCode);

			if (wList != null && wList.size() > 0) {
				wResult = wList.get(0);
			}
		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("MSS_QueryMeasureUnit", ex);
		}
		return wResult;
	}

	public MSSMeasureUnit MSS_CheckMeasureUnit(BMSEmployee wLoginUser, MSSMeasureUnit wMSSMeasureUnit,
			OutResult<Integer> wErrorCode) {
		MSSMeasureUnit wMeasureUnit = new MSSMeasureUnit();
		wErrorCode.set(0);

		try {

			// Step0:查询
			Map<String, Object> wParwms = new HashMap<String, Object>();
			String wSQLText = StringUtils.Format(
					"Select * from {0}.mss_measureunit "
							+ " where ID!=:ID and ( MeasureUnitID =:MeasureUnitID and   SourceUnitID=:SourceUnitID) ",
					super.getDBName());
			wParwms.clear();
			wParwms.put("ID", wMSSMeasureUnit.ID);
			wParwms.put("SourceUnitID", wMSSMeasureUnit.SourceUnitID);
			wParwms.put("MeasureUnitID", wMSSMeasureUnit.MeasureUnitID);

			wSQLText = this.DMLChange(wSQLText);
			List<Map<String, Object>> wQueryResultList = super.queryForList(wSQLText, wParwms);
			for (Map<String, Object> wSqlDataReader : wQueryResultList) {
				wMeasureUnit.ID = StringUtils.parseInt(wSqlDataReader.get("ID"));
				wMeasureUnit.SourceUnitID = StringUtils.parseInt(wSqlDataReader.get("SourceUnitID"));
				wMeasureUnit.MeasureUnitID = StringUtils.parseInt(wSqlDataReader.get("MeasureUnitID"));

				wMeasureUnit.CreatorID = StringUtils.parseInt(wSqlDataReader.get("CreatorID"));
				wMeasureUnit.CreateTime = StringUtils.parseCalendar(wSqlDataReader.get("CreateTime"));
				wMeasureUnit.EditorID = StringUtils.parseInt(wSqlDataReader.get("EditorID"));
				wMeasureUnit.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));
				wMeasureUnit.Active = StringUtils.parseInt(wSqlDataReader.get("Active"));

			}

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("MSS_CheckMeasureUnit", ex);
		}
		return wMeasureUnit;
	}

}